package com.example.kliven.because.activity;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.kliven.because.R;
import com.example.kliven.because.adapter.ContactAdapter;
import com.example.kliven.because.bean.ContactBean;

import java.util.ArrayList;

public class PhoneRecommendActivity extends Activity implements AdapterView.OnItemLongClickListener {
    private ArrayList<ContactBean> contactBeans = new ArrayList();
    private ListView listView;
    private ContactAdapter contactAdapter;
    private ContentResolver contentResolver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_recommend);
        getLocalContact();
        listView = (ListView) findViewById(R.id.fragment_free_listview);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String number = contactBeans.get(position).getPnonenumber();
                String body = "我正在使用“因味”App，你也加入吧。http://www.pc6.com/down.asp?id=188781 这里可以下载哦！";
//                SmsManager smsManager = SmsManager.getDefault();
//                smsManager.sendTextMessage(number, null, body, null, null);
//                Snackbar.make(show_msg_sended,"邀请信息已发送",Snackbar.LENGTH_SHORT).show();
                Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
                sendIntent.setData(Uri.parse("smsto:" + number));
                sendIntent.putExtra("sms_body", body);
                startActivity(sendIntent);
            }
        });
        contactAdapter = new ContactAdapter(contactBeans,this);
        listView.setAdapter(contactAdapter);
        listView.setOnItemLongClickListener(this);
    }

    private void getLocalContact() {
        contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);

        int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);

        int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
        while (cursor.moveToNext()){
            String contactId = cursor.getString(idColumn);
            String displayName = cursor.getString(nameFieldColumnIndex);

            int phoneCount = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            if(phoneCount > 0){
                Cursor phone = this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null
                        , ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null);

//                cursor.close();
                while (phone.moveToNext()){

                    ContactBean bean = new ContactBean();
                    String strPhoneNumber = phone.
                            getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    bean.setHeadimage(GetLinkManPhoto(phone));

                    bean.setPnonename(displayName);
                    bean.setPnonenumber(getNumber(strPhoneNumber));
                    contactBeans.add(bean);
                }
            }
        }
    }
    private Bitmap GetLinkManPhoto(Cursor phone) {
        String photoid = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID));
        if (photoid != null) {
            Cursor c =this.getContentResolver().query(
                    ContactsContract.Data.CONTENT_URI,
                    new String[] { ContactsContract.Data.DATA15 },
                    ContactsContract.Data._ID + "=" + photoid, null, null);
            c.moveToFirst();
            byte[] ss = c.getBlob(0);
            if (ss != null) {
                Bitmap map = BitmapFactory.decodeByteArray(ss, 0, ss.length);
                c.close();
                return map;
            }
            c.close();
            return null;
        }
        return null;

    }

    public static  String getNumber(String num){
        if (num != null){
            num = num.replaceAll("-","");
            num = num.replaceAll(" ","");
            if (num.startsWith("+86")){
                num = num.substring(3);
            }else if(num.startsWith("86")){
                num = num.substring(2);
            }else if (num.startsWith("17951")) {
                num = num.substring(5);
            } else if (num.startsWith("12593")) {
                num = num.substring(5);
            }
        }
        return num.trim();
    }
    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        return false;
    }
}
